package com.company;

import org.omg.CORBA.TRANSACTION_MODE;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
import java.util.Timer;

public class Main {
    public static final int WIDTH=400;
    public static final int HEIGHT=654;

    public static final int START=0;
    public static final int RUNNING=1;
    public static final int PAUSE=2;
    public static final int GAMEOVER=3;

    public static int status=0;

    public static int score; //分数
    public static Timer timer; //计时器
    public static int interval=10; //时间单位(毫秒)

    //难度
    private static int difficult=50;

    public static Hero hero=new Hero(); //英雄机
    public static List<Flyer> flyers=new LinkedList<>(); //飞行物
    public static List<Bullet> bullets=new LinkedList<>(); //子弹

    public static MyPanel panel=new MyPanel();

    /**
     * 画分数
     */
    public static void drawScore(Graphics g){
        //分数所在坐标
        int x=10;
        int y=15;
        g.setFont(new Font("微软雅黑",Font.BOLD,17));
        g.setColor(Color.RED);
        g.drawString("分数："+score,x,y);
        y+=20;
        g.drawString("生命："+hero.getLife(),x,y);
    }

    /**
     * 画状态
     */
    public static void drawStatus(Graphics g){
        switch (status){
            case START:g.drawImage(Image.start,0,0,null);break;
            case PAUSE:g.drawImage(Image.pause,0,0,null);break;
            case GAMEOVER:g.drawImage(Image.gameover,0,0,null);break;
        }
    }

    /**
     * 画飞行物
     */
    public static void drawFlyer(Graphics g){
        for(Flyer flyer:flyers){
            g.drawImage(flyer.getImage(),flyer.x,flyer.y,null);
        }
    }

    /**
     * 画英雄
     */
    public static void drawHero(Graphics g){
        g.drawImage(hero.getImage(),hero.x,hero.y,null);
    }

    /**
     * 画子弹
     */
    public static void drawBullet(Graphics g){
        for(Bullet bullet:bullets){
            g.drawImage(bullet.getImage(),bullet.x,bullet.y,null);
        }
    }

    public static void main(String[] args) {
        // write your code here
        JFrame frame=new JFrame("天天打飞gay");
        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(WIDTH,HEIGHT);
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);

        action();
    }

    //驱动类
    public static void action(){
        MouseAdapter l=new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch (status){
                    case START:
                        status=RUNNING;//待启动状态转换到启动状态
                        break;
                    case GAMEOVER:
                        //清理各种现场
                        flyers=new LinkedList<>();
                        bullets=new LinkedList<>();
                        hero=new Hero();
                        status=0;
                        score=0;
                        break;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                if(status==PAUSE){
                    status=RUNNING;
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if(status==RUNNING){
                    status=PAUSE;
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if(status==RUNNING){
                    hero.move(e.getX(),e.getY());
                }
            }
        };
        panel.addMouseListener(l); //鼠标点击监听
        panel.addMouseMotionListener(l); //鼠标移动监听

        timer=new Timer();
        //时间驱动
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(status==RUNNING){
                    makePlane(); //制作敌机
                    launch(); //发射子弹
                    step(); //前进一步
                    crash(); //进行碰撞检测
                    deleteOutBound();//清除越界
                    isEnd(); //判断是否已经结束了

                    panel.repaint();//重绘
                }
            }
        },interval,interval);
    }

    /**
     * 走下一步
     */
    public static void step(){
        hero.step();
        for(Flyer flyer:flyers){
            flyer.step();
        }

        for(Bullet bullet:bullets){
            bullet.step();
        }
    }

    /**
     * 检测碰撞并统计得分
     */
    public static void crash(){
        crashBullet();
        crashPlane();
    }

    /**
     * 检测子弹与飞行物的碰撞
     */
    public static void crashBullet(){
        for(int i=0;i<bullets.size();i++){
            int index=bullets.get(i).boom(flyers); //判断是否发生了碰撞

            //发生碰撞则消去子弹和飞行物,增加分数
            if(index!=-1){
                bullets.remove(i--);
                if(flyers.get(index) instanceof Bee){
                    Bee bee=(Bee)flyers.get(index);
                    if(bee.getReward()==Bee.DOUBLEFIRE){
                        hero.setDoubleFire(true);
                    }else {
                        hero.addLife();
                    }
                }
                flyers.remove(index);
                score+=5;
            }
        }
    }

    /**
     * 检测飞机与飞行物的碰撞
     */
    public static void crashPlane(){
        for(int i=0;i<flyers.size();i++){
            if(hero.hit(flyers.get(i))){
                hero.subtractLife();
                flyers.remove(i);
                i--;
                break;
            }
        }
    }

    /**
     * 制作敌机
     */
    public static int flyerIndex=0;
    public static void makePlane(){
        flyerIndex++;
        if(flyerIndex%difficult==0){
            Random random=new Random();
            if(random.nextInt(20)==0){
                int x=random.nextInt(WIDTH-Image.bee.getWidth());
                flyers.add(new Bee(x,-Image.bee.getHeight()));
            }else {
                int x=random.nextInt(WIDTH-Image.airplane.getWidth());
                flyers.add(new Plane(x,-Image.airplane.getHeight()));
            }
        }
        if(flyerIndex%200==0){
            difficult--;
        }
    }


    /**
     * 删除越界元素
     */
    public static void deleteOutBound(){
        for(int i=0;i<flyers.size();i++){
            if(flyers.get(i).outOfBound()){
                flyers.remove(i);
                i--;
            }
        }

        for(int i=0;i<bullets.size();i++){
            if(bullets.get(i).outOfBound()){
                bullets.remove(i);
                i--;
            }
        }
    }

    /**
     * 判断游戏是否结束,若是则转换状态
     */
    public static void isEnd(){
        if(hero.getLife()<=0){
            status=GAMEOVER;
        }
    }

    /**
     * 子弹发射
     */
    public static int launch=0;
    public static void launch(){
        launch++;
        if(launch%40==0){
            hero.fire(bullets);
        }
    }

    //画板类
    private static class MyPanel extends JPanel{
        @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(Image.background,0,0,null);
            drawStatus(g);
            drawBullet(g);
            drawFlyer(g);
            drawHero(g);
            drawScore(g);
        }
    }
}